package ebcs.scan.controller.capacityCtr;

import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;

import ebcs.database.bean.BsCapacity;
import ebcs.database.bean.BsNeedInfor;
import ebcs.database.bean.BsNeedStage;
import ebcs.database.bean.BsSaleNeed;
import ebcs.database.bean.RpProjectMoneyBean;
import ebcs.database.service.BsCapacityServiceImp;
import ebcs.database.service.BsUserInforServiceImp;
import ebcs.scan.bean.common.ExcleEntity;
import ebcs.scan.common.utils.ExportDataUtil;
import jplat.core.trans.JAppBaseService;
import jplat.core.trans.JAppContext;
import jplat.error.exception.JSystemException;
import jplat.error.exception.JTransException;
import jplat.tools.string.DateUtil;

/**
 * 产能分配
 * @author JMY
 *
 */
@Controller
public class BsCapacityCtrl extends JAppBaseService {

	private Logger logger = Logger.getLogger(BsCapacityCtrl.class);
	
	@Autowired 
	public BsCapacityServiceImp capacityServiceImp ; 
	/**
	 * 产能分配数据查询
	 */
	@RequestMapping("/page/capacity/searchCapacityInfor.do")
	public void searchCapacityInfor( HttpServletRequest request,HttpServletResponse response ) throws JTransException, JSystemException{
		JAppContext context = serviceFactory.buildAppContextNoUser(request,response);
		BsCapacity req = context.obtainReqModel(BsCapacity.class);
 		String endRow = ((Integer.parseInt(req.getStartRow()))+(Integer.parseInt(req.getPageSize())))+"";
 		req.setEndRow(endRow);
		List<BsCapacity> capacityList = this.capacityServiceImp.searchCapacityInfor(req);
		int count = this.capacityServiceImp.searchCapacityInforPageCount(req);
		Map rsModel = new HashMap();
		rsModel.put("count", count);
  		rsModel.put("capacityList", capacityList);
 		context.buildAppResponse(rsModel);
	}
	
	/**
	 * 产能分配操作
	 * 
	 * @param request
	 * @param response
	 * @param userNo
	 * @return
	 * @throws JTransException
	 * @throws JSystemException
	 */
	@RequestMapping("/page/capacity/{needId}/assignCapacity.do")
	public String assignCapacity(HttpServletRequest request, HttpServletResponse response,
			@PathVariable String needId) throws JTransException, JSystemException {
		BsCapacity req =new BsCapacity();
		req.setNeedId(needId);
		BsCapacity bsCapacity = this.capacityServiceImp.selectBsCapacity(req);
		List<BsCapacity> userInfoList = this.capacityServiceImp.assignCapacity(req);
		List<BsCapacity> assignCapacityHistory = this.capacityServiceImp.assignCapacityHistory(req);
		request.setAttribute("bsCapacity", bsCapacity);
		request.setAttribute("userInfoList", userInfoList);
		request.setAttribute("assignCapacityHistory", assignCapacityHistory);
		return "main/report/assignCapacity";
	}
	
	/**
	 * 产能分配报表查询
	 */
	@RequestMapping("/page/capacity/searchCapacityForms.do")
	public void searchCapacityForms( HttpServletRequest request,HttpServletResponse response ) throws JTransException, JSystemException{
		JAppContext context = serviceFactory.buildAppContextNoUser(request,response);
		BsCapacity req = context.obtainReqModel(BsCapacity.class);
 		String endRow = ((Integer.parseInt(req.getStartRow()))+(Integer.parseInt(req.getPageSize())))+"";
 		req.setEndRow(endRow);
		List<BsCapacity> capacityList = this.capacityServiceImp.searchCapacityForms(req);
		int count = this.capacityServiceImp.searchCapacityFormsPageCount(req);
		Map rsModel = new HashMap();
		rsModel.put("count", count);
  		rsModel.put("capacityList", capacityList);
 		context.buildAppResponse(rsModel);
	}
	
	/**产能分配报表下载
	 * @param request
	 * @param response
	 * @throws JTransException
	 * @throws JSystemException
	 */
	@RequestMapping("/page/capacity/capacityFormsdownloadExcel.do")
	public void capacityFormsdownloadExcel(HttpServletRequest request, HttpServletResponse response)
			throws JTransException, JSystemException {
		JAppContext context = serviceFactory.buildAppContextNoUser(request, response);// 自动客户信息
		BsCapacity req = context.obtainReqModel(BsCapacity.class);

		List<BsCapacity> capacityList = this.capacityServiceImp.searchCapacityFormsAllInfor(req);
		String path = request.getServletContext().getRealPath("");// 获取项目动态绝对路径
		ExportDataUtil export = new ExportDataUtil();
		ExcleEntity entity = new ExcleEntity();
		entity.setTitle("产能分配报表");
		entity.setFileName("产能分配报表.xls");
		entity.setPath(path);
		Map<Object, String> headMap = new LinkedHashMap<Object, String>();
		headMap.put("needNo","需求编号");
		headMap.put("needName","需求名称");
		headMap.put("userName","姓名");
		headMap.put("proTeamName","项目组");
		headMap.put("renyue","需求人月");
		headMap.put("workload","已完成工作量");
		headMap.put("sumHappenDate","已报工天数");
		headMap.put("startDate","开始报工时间");
		headMap.put("latestDate","最新报工时间");
		entity.setHeadMap(headMap);
		export.exportExcel(entity, capacityList);
		context.buildAppResponse(entity);
	}
	
	/**
	 * 产能排名总报表查询
	 */
	@RequestMapping("/page/capacity/searchCapacityFormsAll.do")
	public void searchCapacityFormsAll( HttpServletRequest request,HttpServletResponse response ) throws JTransException, JSystemException{
		JAppContext context = serviceFactory.buildAppContextNoUser(request,response);
		BsCapacity req = context.obtainReqModel(BsCapacity.class);
 		String endRow = ((Integer.parseInt(req.getStartRow()))+(Integer.parseInt(req.getPageSize())))+"";
 		req.setEndRow(endRow);
		List<BsCapacity> capacityList = this.capacityServiceImp.searchCapacityFormsRank(req);
		int count = this.capacityServiceImp.searchCapacityFormsRankPageCount(req);
		Map rsModel = new HashMap();
		rsModel.put("count", count);
  		rsModel.put("capacityList", capacityList);
 		context.buildAppResponse(rsModel);
	}
	
	/**产能排名总报表下载
	 * @param request
	 * @param response
	 * @throws JTransException
	 * @throws JSystemException
	 */
	@RequestMapping("/page/capacity/capacityFormsAlldownloadExcel.do")
	public void capacityFormsAlldownloadExcel(HttpServletRequest request, HttpServletResponse response)
			throws JTransException, JSystemException {
		JAppContext context = serviceFactory.buildAppContextNoUser(request, response);// 自动客户信息
		BsCapacity req = context.obtainReqModel(BsCapacity.class);

		List<BsCapacity> capacityList = this.capacityServiceImp.searchCapacityFormsRankAll(req);
		String path = request.getServletContext().getRealPath("");// 获取项目动态绝对路径
		ExportDataUtil export = new ExportDataUtil();
		ExcleEntity entity = new ExcleEntity();
		entity.setTitle("产能排名总报表");
		entity.setFileName("产能排名总报表.xls");
		entity.setPath(path);
		Map<Object, String> headMap = new LinkedHashMap<Object, String>();
		headMap.put("userName","姓名");
		headMap.put("proTeamName","项目组");
		headMap.put("workloadYear","年度工作总量");
		headMap.put("workloadMonth","月度工作总量");
		headMap.put("rankYear","年度排名");
		headMap.put("rankMonth","当月工作量排名");
		headMap.put("months","时间");
		entity.setHeadMap(headMap);
		export.exportExcel(entity, capacityList);
		context.buildAppResponse(entity);
	}
	
	/**
	 * 产能周度排名查询
	 */
	@RequestMapping("/page/capacity/searchCapacityWeekInfor.do")
	public void searchCapacityWeekInfor( HttpServletRequest request,HttpServletResponse response ) throws JTransException, JSystemException{
		JAppContext context = serviceFactory.buildAppContextNoUser(request,response);
		BsCapacity req = context.obtainReqModel(BsCapacity.class);
 		String endRow = ((Integer.parseInt(req.getStartRow()))+(Integer.parseInt(req.getPageSize())))+"";
 		req.setEndRow(endRow);
		List<BsCapacity> capacityList = this.capacityServiceImp.searchCapacityWeekInfor(req);
		int count = this.capacityServiceImp.searchCapacityWeekInforPageCount(req);
		Map rsModel = new HashMap();
		rsModel.put("count", count);
  		rsModel.put("capacityList", capacityList);
 		context.buildAppResponse(rsModel);
	}
	
	/**产能周度排名下载
	 * @param request
	 * @param response
	 * @throws JTransException
	 * @throws JSystemException
	 */
	@RequestMapping("/page/capacity/capacityWeekInfordownloadExcel.do")
	public void capacityWeekInfordownloadExcel(HttpServletRequest request, HttpServletResponse response)
			throws JTransException, JSystemException {
		JAppContext context = serviceFactory.buildAppContextNoUser(request, response);// 自动客户信息
		BsCapacity req = context.obtainReqModel(BsCapacity.class);

		List<BsCapacity> capacityList = this.capacityServiceImp.searchCapacityWeekInforAll(req);
		String path = request.getServletContext().getRealPath("");// 获取项目动态绝对路径
		ExportDataUtil export = new ExportDataUtil();
		ExcleEntity entity = new ExcleEntity();
		entity.setTitle("产能周度排名总报表");
		entity.setFileName("产能周度排名总报表.xls");
		entity.setPath(path);
		Map<Object, String> headMap = new LinkedHashMap<Object, String>();
		headMap.put("userName","姓名");
		headMap.put("proTeamName","项目组");
		headMap.put("workloadThis","上周工作总量");
		headMap.put("workloadBefore","本周工作总量");
		headMap.put("rankThis","上周工作量排名");
		headMap.put("rankBefore","当月工作量排名");
		entity.setHeadMap(headMap);
		export.exportExcel(entity, capacityList);
		context.buildAppResponse(entity);
	}
	
	/**
	 * 新增产能分配
	 */
	@RequestMapping("/page/capacity/insertCapacity.do")
	public void insertCapacity( HttpServletRequest request,HttpServletResponse response ) throws JTransException, JSystemException{
		JAppContext context = serviceFactory.buildAppContextNoUser(request,response);		//buildAppContextWithUser 自带客户信息.
		BsCapacity req = context.obtainReqModel(BsCapacity.class);
		String rouid = UUID.randomUUID().toString().replaceAll("-", "");
		req.setRouId(rouid);
		capacityServiceImp.insertCapacity(req);
		Map rsModel = new HashMap();
		context.buildAppResponse(rsModel);
	}
}
